package com.easy.order.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.easy.common.bean.Payment;
import com.easy.common.service.IPaymentService;
import com.easy.common.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("order/payment")
@CrossOrigin
public class PaymentController {

    @Autowired
    IPaymentService paymentService;

    // 根据id获取支付记录信息
    @GetMapping("/{id}")
    public Result getPaymentById(@PathVariable Integer id) {
        return Result.success(paymentService.getPaymentById(id));
    }

    // 根据用户id获取支付记录列表
    @GetMapping("/user/{userId}")
    public Result getPaymentsByUserId(@PathVariable Integer userId) {
        List<Payment> payments = paymentService.getPaymentsByUserId(userId);
        return Result.success(payments);
    }

    // 根据秒杀商品id获取支付记录列表
    @GetMapping("/seckillgoods/{seckillGoodsId}")
    public Result getPaymentsBySeckillGoodsId(@PathVariable Integer seckillGoodsId) {
        List<Payment> payments = paymentService.getPaymentsBySeckillGoodsId(seckillGoodsId);
        return Result.success(payments);
    }

    // 根据商品id获取支付记录列表
    @GetMapping("/goods/{goodsId}")
    public Result getPaymentsByGoodsId(@PathVariable Integer goodsId) {
        List<Payment> payments = paymentService.getPaymentsByGoodsId(goodsId);
        return Result.success(payments);
    }

    // 添加支付记录
    @PostMapping("/add")
    public Result addPayment(@RequestBody Payment payment) {
        int result = paymentService.addPayment(payment);
        return result > 0 ? Result.success("添加成功") : Result.fail(500, "添加失败");
    }

    // 更新支付记录
    @PutMapping("/update")
    public Result updatePayment(@RequestBody Payment payment) {
        int result = paymentService.updatePayment(payment);
        return result > 0 ? Result.success("更新成功") : Result.fail(500, "更新失败");
    }

    // 删除支付记录（软删除）
    @DeleteMapping("/delete/{id}")
    public Result deletePayment(@PathVariable Integer id) {
        int result = paymentService.deletePayment(id);
        return result > 0 ? Result.success("删除成功") : Result.fail(500, "删除失败");
    }

    // 获取支付记录列表
    @GetMapping("/list")
    public Result getPaymentList(Payment payment) {
        return Result.success(paymentService.getAll(payment));
    }

    // 获取支付记录分页列表
    @GetMapping("/page")
    public Result getPaymentPage(Payment payment, Integer current, Integer size) {
        IPage<Payment> page = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(current, size);
        return Result.success(paymentService.getPage(payment, page));
    }
}
